package com.sh.rbac.handler;


import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.sh.rbac.utils.SecurityHolderUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 自动设置创建人，创建时间，修改人，修改时间默认值
 *
 * @author lucky
 */
@Component
public class DefaultDBFieldHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        this.setFieldValByName("createTime", LocalDateTime.now(), metaObject);
        this.setFieldValByName("createBy", SecurityHolderUtils.getUserId(), metaObject);
        this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
        this.setFieldValByName("updateBy", SecurityHolderUtils.getUserId(), metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
        setFieldValByName("updateBy", SecurityHolderUtils.getUserId(), metaObject);
    }
}
